﻿package elements.axis ;
	import flash.text.TextField;
	
	class YAxisLabelsRight extends YAxisLabelsBase {
		
		public function new( parent:YAxisRight, json:Dynamic ) {
			
			var values:Array<Dynamic>;
			var ok:Bool = false;
			var lblText:String = "#val#";
			
			if( json.y_axis_right )
			{
				if(Std.is(json.y_axis_right.labels,Array ))
				{
					values = [];
					
					// use passed in min if provided else zero
					var i: Float = (json.y_axis_right && json.y_axis_right.min) ? json.y_axis_right.min : 0;
					for ( s in json.y_axis_right.labels )
					{
						values.push( { val:s, pos:i } );
						i++;
					}
					
					//
					// alter the MinMax object:
					//
					// use passed in max if provided else the number of values less 1
					i = (json.y_axis_right && json.y_axis_right.max) ? json.y_axis_right.max : values.length - 1;
					parent.set_y_max( i );
					ok = true;
				}
				else if (Std.is(json.y_axis_right.labels,Dynamic )) 
				{
					i = (json.y_axis_right && json.y_axis_right.min) ? json.y_axis_right.min : 0;
					if (Std.is(json.y_axis_right.labels.text,String )) lblText = json.y_axis_right.labels.text;

					if (Std.is(json.y_axis_right.labels.labels,Array ))
					{
						values = [];
						for ( obj in json.y_axis_right.labels.labels )
						{
							if (Std.is(obj,Number)) 
							{
								values.push( { val:lblText, pos:obj } );
								i = (obj > i) ? cast(obj,Number): i;
							}
							else if (Std.is(obj.y,Number))
							{
								s = (Std.is(obj.text,String)) ? obj.text : lblText;
								var lblStyle:Dynamic = { val:s, pos:obj.y }
								if (obj.color != null) lblStyle.color = obj.color;
								if (obj.size != null) lblStyle.size = obj.size;
								if (obj.rotate != null) lblStyle.rotate = obj.rotate;
								values.push( lblStyle );
								i = (obj.y > i) ? obj.y : i;
							}
						}
						ok = true;
					}
				}				
			}
			
			if( !ok && parent.style.visible )
				values = make_labels( parent.style.min, parent.style.max, true, 1, lblText );
			
			super( values, 1, json, 'y_label_2_', 'y_axis_right');
		}

		// move y axis labels to the correct x pos
		public override function resize( left: Float, box:ScreenCoords ):Void {
			var maxWidth: Float = this.get_width();
			var i: Float;
			var tf:YTextField;
			
			for ( i in 0...this.numChildren ) {
				// left align
				tf = cast(this.getChildAt(i),YTextField);
				tf.x = left; // - tf.width + maxWidth;
			}
			
			// now move it to the correct Y, vertical center align
			for ( i in 0...this.numChildren ) {
				tf = cast(this.getChildAt(i),YTextField);
				if (tf.rotation != 0) {
					tf.y = box.get_y_from_val( tf.y_val, true ) + (tf.height / 2);
				}
				else {
					tf.y = box.get_y_from_val( tf.y_val, true ) - (tf.height / 2);
				}
				if (tf.y < 0 && box.top == 0) // Tried setting tf.height but that didnt work 
					tf.y = (tf.rotation != 0) ? tf.height : tf.textHeight - tf.height;
			}
		}
	}
